
package com.hlkj.pay.service.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.dto.common.CommonAppQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.common.CommonAppMapper;
import com.hlkj.pay.infrastructure.model.common.CommonAppDO;
import com.hlkj.pay.service.common.ICommonAppConfigService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/09/27 13:10
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommonAppConfigServiceImpl implements ICommonAppConfigService {
    private final CommonAppMapper commonAppMapper;
    @Override
    public PageResult<CommonAppDO> queryPage(CommonAppQueryDto commonAppQueryDto) {
        return commonAppMapper.selectPage(commonAppQueryDto, buildCommonAppWrapper(commonAppQueryDto));
    }

    @Override
    public List<CommonAppDO> queryList(CommonAppQueryDto commonAppQueryDto) {
        return commonAppMapper.selectList(buildCommonAppWrapper(commonAppQueryDto));
    }
    @Override
    public List<CommonAppDO> queryListByIds(List<Long> ids) {
        return commonAppMapper.selectBatchIds(ids);
    }
    
    @Override
    public CommonAppDO detail(Long id) {
        return commonAppMapper.selectById(id);
    }

    @Override
    public CommonAppDO detail(String appId,CommonEnum.COMMON_APP_TYPE commonAppType) {
        return commonAppMapper.selectOne(CommonAppDO::getAppId,appId,CommonAppDO::getType,commonAppType.getCode());
    }

    @Override
    public void addCommonApp(CommonAppDO commonAppDO) {
        commonAppMapper.insert(commonAppDO);
    }

    @Override
    public void updateCommonApp(CommonAppDO commonAppDO) {
        commonAppMapper.updateById(commonAppDO);
    }

    @Override
    public void statusUpdate(Long id, CommonEnum.YES_NO_TYPE yesNoType) {
        CommonAppDO commonAppDO=new CommonAppDO();
        commonAppDO.setId(id);
        commonAppDO.setStatus(yesNoType.getCode());
        commonAppMapper.updateById(commonAppDO);
    }

    @Override
    public void deleteCommonApp(Long id) {
        commonAppMapper.deleteById(id);
    }
    
    /**
     * 查询条件封装
     *
     * @param commonAppQueryDto
     * @return
     */
    LambdaQueryWrapper<CommonAppDO> buildCommonAppWrapper(CommonAppQueryDto commonAppQueryDto) {
        LambdaQueryWrapperX<CommonAppDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(CommonAppDO::getAppId, commonAppQueryDto.getAppId());
        queryWrapper.inIfPresent(CommonAppDO::getId, commonAppQueryDto.getIds());
        queryWrapper.eqIfPresent(CommonAppDO::getId, commonAppQueryDto.getId());
        queryWrapper.inIfPresent(CommonAppDO::getId, commonAppQueryDto.getIds());
        queryWrapper.eqIfPresent(CommonAppDO::getStatus, commonAppQueryDto.getStatus());
        queryWrapper.eqIfPresent(CommonAppDO::getType, commonAppQueryDto.getType());
        queryWrapper.eqIfPresent(CommonAppDO::getName, commonAppQueryDto.getName());
        queryWrapper.likeIfPresent(CommonAppDO::getName, commonAppQueryDto.getLikeName());
        return queryWrapper;
    }
}
